package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.vo.UserOrgVo;
import org.jeecg.common.util.ListUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysOrganization;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysOrganizationMapper;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.jeecg.modules.system.service.ISysOrganizationService;
import org.jeecg.modules.system.vo.SysOrganizationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: sys_organization
 * @Author: jeecg-boot
 * @Date:   2021-10-29
 * @Version: V1.0
 */
@Service
@Slf4j
public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMapper, SysOrganization> implements ISysOrganizationService {

    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    SysOrganizationMapper sysOrganizationMapper;

    @Override
    public void queryParentIdsById(List<String> parentIds, String organizationId) {
        SysOrganization sysOrganization = sysOrganizationMapper.selectById(organizationId);

        if (null != sysOrganization && oConvertUtils.isNotEmpty(sysOrganization.getParentId())) {
            parentIds.add(sysOrganization.getParentId());
            queryParentIdsById(parentIds, sysOrganization.getParentId());
        }
    }

    @Override
    public void queryChildren(SysOrganizationVO sysOrganizationVO) {
        LambdaQueryWrapper<SysOrganization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysOrganization::getParentId,sysOrganizationVO.getOrganizationId());
        List<SysOrganization> children = this.list(queryWrapper);
        if (oConvertUtils.listIsEmpty(children)){
            return;
        }
        ListUtils<SysOrganizationVO> utils = BeanUtils.instantiate(ListUtils.class);
        List<SysOrganizationVO> childrenVO = new ArrayList<>();
        utils.copyList(children, childrenVO, SysOrganizationVO.class);
        for (SysOrganizationVO childVO: childrenVO) {
            queryChildren(childVO);
        }
        sysOrganizationVO.setChildren(childrenVO);
    }

    @Override
    public Result<SysOrganization> queryByUserId(String userId) {
        Result<SysOrganization> result = new Result<>();
        SysUser sysUser = sysUserMapper.selectById(userId);
        if (oConvertUtils.isEmpty(sysUser)) {
            return new Result<SysOrganization>().error500("没有此用户");
        }
        SysOrganization sysOrganization = this.getById(sysUser.getOrganizationId());
        if (oConvertUtils.isEmpty(sysOrganization)) {
            result.error500("未找到机构信息");
        } else {
            result.setResult(sysOrganization);
            result.setCode(CommonConstant.SC_OK_200);
            result.setSuccess(true);
        }
        return result;
    }

    @Override
    public UserOrgVo queryOrgs(String userId, Boolean needParents) {
        SysOrganization org = this.queryByUserId(userId).getResult();
        List<String> parentIds = null;
        UserOrgVo result = new UserOrgVo();
        if (org == null) {
            return null;
        }
        if (StringUtils.isNotBlank(org.getOrganizationId()) && needParents) {
            parentIds = new ArrayList<>();
            try {
                this.queryParentIdsById(parentIds, org.getParentId());
            } catch (Exception e) {
                log.info("获取上级机构异常");
                result.setParentIds(new ArrayList<>());
            }
            result.setParentIds(parentIds);
        }
        result.setOrgId(org.getOrganizationId());

        return result;
    }
}
